
import { Component, OnInit, Input } from '@angular/core';
import { TreeNode, SelectItem } from 'primeng/api';
import { DialogService, ConfirmationService } from 'primeng/primeng';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService } from '@angular-mdl/core';
import { TaskMenuService } from './taskmenu.service';
import { TaskMenuGroupComponent } from './taskmenu-group.component';
@Component({
  selector: 'tasklist-config-menu',
  templateUrl: './taskmenu.component.html',
  styleUrls: ['./taskmenu.component.scss'],
  providers: [TaskMenuService, ConfirmationService]
})
export class TaskMenuComponent implements OnInit {
  @Input()
  systemUrl: string = "";
  currentGroupId: number;
  selectedNode: TreeNode;//当前选中节点
  loading: boolean = true;
  menus: TreeNode[];
  menu: any;//当前编辑菜单
  menuForm: FormGroup;

  /** 图标类型 */
  icons: SelectItem[] = new Array(
    { label: 'fa-circle-o', value: 'fa-circle-o' },
    { label: 'fa-cogs', value: 'fa-cogs' },
    { label: 'fa-university', value: 'fa-university' },
    { label: 'fa-database', value: 'fa-database' },
    { label: 'fa-address-book-o', value: 'fa-address-book-o' }
  );
  /** 菜单样式 */
  menuClasses: SelectItem[] = new Array(
    { label: '蓝色', value: 'ui-button-info' },
    { label: '灰色', value: 'ui-button-secondary' },
    { label: '绿色', value: 'ui-button-success' },
    { label: '黄色', value: 'ui-button-warning' },
    { label: '红色', value: 'ui-button-danger' }
  );
  /** 请求方式 */
  requestTypes: SelectItem[] = new Array(
    { label: '自定义', value: 0 },
    { label: 'Ajax请求', value: 1 },
    { label: '文件下载', value: 2 },
    { label: '打开窗口', value: 3 },
    { label: '路由跳转', value: 4 },
  );
  /** 响应方式 */
  responseTypes: SelectItem[] = new Array(
    { label: '自定义', value: 0 },
    { label: '刷新列表', value: 1 }
  );
  constructor(
    private fb: FormBuilder,
    private dialogService: DialogService,
    private taskMenuService: TaskMenuService,
    private confirmationService: ConfirmationService,
    private mdlSnackbarService: MdlSnackbarService,
  ) { }

  ngOnInit() {
    this.loadTaskListMenuTree();
  }

  /**加载操作项 */
  loadTaskListMenuTree() {
    this.loading = true;
    this.taskMenuService.listMenuTree(this.systemUrl).subscribe(menus => {
      this.loading = false;
      this.menus = menus;
      if (this.menus.length > 0 && this.menus[0].children) {
        this.selectedNode = this.menus[0].children[0];
        this.nodeSelect();
      }
    });
  }
  /**保存或更新菜单分组 */
  editGroup(optFlag) {
    let params = {};
    if ('edit' == optFlag) {
      params['groupId'] = this.selectedNode['id'].replace('grp_', '') || '';
      params['groupName'] = this.selectedNode.label || '';
    }
    const ref = this.dialogService.open(TaskMenuGroupComponent, {
      'header': 'edit' == optFlag ? '编辑分组' : '新增分组',
      'width': '400px',
      contentStyle: { "height": "200px" },
      data: params
    });

    ref.onClose.subscribe((data: any) => data && this.loadTaskListMenuTree());
  }

  /**删除分组 */
  deleteGroup() {
    this.confirmationService.confirm({
      message: '您确定要删除选择的分组吗?',
      header: '确认',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.taskMenuService.deleteGroup(this.selectedNode['id'].replace('grp_', ''), this.systemUrl).subscribe(data => {
          this.mdlSnackbarService.showToast("删除分组成功");
          this.loadTaskListMenuTree();
        });
      }
    });
  }

  buildFrom() {
    this.menuForm = this.fb.group({
      'menuId': [this.menu.menuId],
      'menuName': [this.menu.menuName, Validators.required],
      'pozAttr': [this.menu.pozAttr],
      'menuIcon': [this.menu.menuIcon],
      'menuClass': [this.menu.menuClass],
      'menuActIndepend': [this.menu.menuActIndepend],
      'requestType': [this.menu.requestType],
      'needConfirm': [this.menu.needConfirm],
      'requestUrl': [this.menu.requestUrl],
      'requestParams': [this.menu.requestParams],
      'responseType': [this.menu.responseType],
      'calcUrl': [this.menu.calcUrl],
      'displayOrder': [this.menu.displayOrder],
      'menuDesc': [this.menu.menuDesc]
    });
  }

  nodeSelect() {
    if (!this.selectedNode) return;
    if (this.selectedNode['rootFlag']) {
      this.currentGroupId = this.selectedNode['id'].replace('grp_', '');
    } else {
      this.taskMenuService.loadMenu(this.selectedNode['id'], this.systemUrl).subscribe(menu => {
        this.menu = menu;
        this.currentGroupId = this.menu.groupId;
        let pozAttr = this.menu.pozAttr || "";
        debugger
        this.menu.pozAttr = this.getPozAttr(pozAttr);
        this.buildFrom();
      });
    }
  }

  /** 新增操作项 */
  addMenu() {
    this.menu = {};
    this.buildFrom();
  }

  /** 保存操作项 */
  saveMenu() {
    debugger

    let value = this.menuForm.value;
    value.menuActIndepend = value.menuActIndepend ? 1 : 0;
    value.needConfirm = value.needConfirm ? 1 : 0;
    value.groupId = this.currentGroupId;

    value.pozAttr = this.readPozAttr(value.pozAttr);

    this.taskMenuService.saveUpdateMenu(value, this.systemUrl).subscribe(data => {
      this.mdlSnackbarService.showToast("菜单保存成功");
      this.loadTaskListMenuTree();
    });
  }

  getPozAttr(pozAttr) {
    let value = [];
    if (pozAttr == 0) {
      value = [];
    } else if (pozAttr == 3) {
      value = ['1', '2'];
    } else {
      value = ['' + pozAttr];
    }
    return value
  }

  readPozAttr(pozAttr) {
    let value = 0;
    if (pozAttr.length == 0) {
      value = 0;
    } else if (pozAttr.length == 1) {
      value = <number>pozAttr[0];
    } else {
      value = 3;
    }
    return value;
  }

  /** 删除操作项 */
  deleteMenu() {
    this.confirmationService.confirm({
      message: '您确定要删除选择的分组吗?',
      header: '确认',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        this.taskMenuService.deleteMenu(this.menu.menuId, this.systemUrl).subscribe(data => {
          this.mdlSnackbarService.showToast("菜单删除成功");
          this.loadTaskListMenuTree();
        });
      }
    });
  }
}
